package net.libra.utils
{
	/**
	 * 
	 * @author elvisjiang
	 * 
	 */
	public class ArrayUtil
	{
					
		public static function arrayContainsValue(arr:Array, value:Object):Boolean
		{
			return (arr.indexOf(value) != -1);
		}	
		
		public static function removeValueFromArray(arr:Array, value:Object):void
		{
			var len:uint = arr.length;
			
			for(var i:Number = len; i > -1; i--)
			{
				if(arr[i] === value)
				{
					arr.splice(i, 1);
				}
			}					
		}
		
		public static function remove(obj:*,from:Array):Boolean
		{
			if(from.indexOf(obj)){
				from.splice(from.indexOf(obj),1);
				return true;
			}else{
				return false;
			}
		}
		/**
		 * 去掉数组中相同的元素
		 */		
		public static function removeSameThing(from:Array):void
		{
			
		}
		
		/**
		 * 根据对象的唯一标识，从数组中查找一个对象
		 */ 
		public static function getObj(property:String,value:Object,from:Array):Object
		{
			for(var i:int = 0;i<from.length;i++){
				if(from[i][property] == value){
					return from[i];
				}
			}
			return null;
		}
		
		/**
		 * 删除数组中属性是property,值为value的对象
		 */ 
		public static function deleteObj(property:String,value:Object,from:Array):void
		{
			for(var i:int = from.length - 1;i>=0;i--){
				if(from[i][property] == value){
					from.splice(i,1);
				}
			}
		}
		
		/**
		 * 根据对象的唯一标识，从数组中得到一个过滤数组
		 */ 
		public static function getObjs(property:String,value:Object,from:Array):Array
		{
			var result:Array = [];
			for(var i:int = 0;i<from.length;i++){
				if(from[i][property] == value){
					result.push(from[i]);
				}
			}
			return result;
		}
		
		public static function createUniqueCopy(a:Array):Array
		{
			var newArray:Array = new Array();
			
			var len:Number = a.length;
			var item:Object;
			
			for (var i:uint = 0; i < len; ++i)
			{
				item = a[i];
				
				if(ArrayUtil.arrayContainsValue(newArray, item))
				{
					continue;
				}
				
				newArray.push(item);
			}
			
			return newArray;
		}		
		
		public static function copyArray(arr:Array):Array
		{	
			return arr.slice();
		}
		
		public static function arraysAreEqual(arr1:Array, arr2:Array):Boolean
		{
			if(arr1.length != arr2.length)
			{
				return false;
			}
			
			var len:Number = arr1.length;
			
			for(var i:Number = 0; i < len; i++)
			{
				if(arr1[i] !== arr2[i])
				{
					return false;
				}
			}
			
			return true;
		}
	}
}
